package com.game.jumpdino;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;

import com.game.jumpdino.R;

public class JumpingPanda extends Activity implements SensorEventListener {	
	protected class ViewHandler implements SwitchViewHandler{
		private boolean isInFinishView;
		
		public void End() {
			finish();
		}

		public void switchToFinish() {
			root.post(new Runnable()
			{
				public void run() {
			        Intent intent = new Intent();
			        intent.setClass(JumpingPanda.this, GameOver.class);
			        finish();
			        startActivity(intent);
				}
			});
			isInFinishView = true;

		}

		public void switchToMain() {
			root.post(new Runnable()
			{
				public void run() {			
				}
			});	
			isInFinishView = false;
		}

		public boolean isInFinishView() {
			return isInFinishView;
		}

		public void showScore(final int score) {
			root.post(new Runnable()
			{
				public void run() {
					String str = Integer.toString(score);
					scoreView.setText(str);
				}
			});
		}

		public void setLife(final int lifes) {
			root.post(new Runnable()
			{
				public void run() {
					if (lifes > 0){
						life1View.setVisibility(0);
					} else {
						life1View.setVisibility(4);
					}
					
					if (lifes > 1){
						life2View.setVisibility(0);
					} else {
						life2View.setVisibility(4);
					}
					
					if (lifes > 2){
						life3View.setVisibility(0);
					} else {
						life3View.setVisibility(4);
					}
				}
			});
		}
	}
	
    private static final int MENU_PAUSE = 1;
    private static final int MENU_RESUME = 2;
   // private static final int MENU_RESTART = 3;
   //  private static final int MENU_STOP = 4;
	
    /** Called when the activity is first created. */
	private JumpingView jumpingView;
	private TextView scoreView;
	private FrameLayout root;
	private ImageView life1View;
	private ImageView life2View;
	private ImageView life3View;
//	private SensorManager mSensorManager;
	
	// @aiyue 0928
	final String tag = "SensorTest";
	SensorManager sm = null;
	private List<Sensor> sensors;
	private Sensor sensor;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
    	// @aiyue 0928
        // get reference to SensorManager
//        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        
        // @aiyue 0928
        sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
        this.sensors = sm.getSensorList(Sensor.TYPE_ACCELEROMETER);
		if (sensors.size() > 0) {
			sensor = sensors.get(0);
		}
		
        
        setContentView(R.layout.main);
        this.root = (FrameLayout)this.findViewById(R.id.root);
        this.jumpingView = (JumpingView)findViewById(R.id.ground);
        this.jumpingView.getThread().viewHandler = new ViewHandler();
        this.scoreView = (TextView)findViewById(R.id.scoreView);
        this.life1View = (ImageView)findViewById(R.id.life1);
        this.life2View = (ImageView)findViewById(R.id.life2);
        this.life3View = (ImageView)findViewById(R.id.life3);
        
    	// @aiyue 0928
//    	mSensorManager.registerListener(this,
//    			mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
//    			SensorManager.SENSOR_DELAY_GAME);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

       // menu.add(0, MENU_RESTART, 0, R.string.menu_restart);
        // menu.add(0, MENU_STOP, 0, R.string.menu_stop);
        menu.add(0, MENU_PAUSE, 0, R.string.menu_pause);
        menu.add(0, MENU_RESUME, 0, R.string.menu_resume);

        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        /* 
        case MENU_RESTART:
            	jumpingView.getThread().restartGame();
                return true;
         
            case MENU_STOP:
            	jumpingView.getThread().stopGame(); 
                return true;
                */
            case MENU_PAUSE:
            	jumpingView.getThread().pause(); 
                return true;
            case MENU_RESUME:
            	jumpingView.getThread().resumeGame();
                return true;
        }

        return false;
    }
    
    @Override
    public void onResume(){
    	super.onResume();
    	// @aiyue 0928
    	// mSensorManager.registerListener(this,
    	//		mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
    	//		SensorManager.SENSOR_DELAY_GAME);
    
    	
    	// if (sensor!=null) {
			sm.registerListener(this, sensor, SensorManager.SENSOR_DELAY_FASTEST);
		// }
    	
    	if (jumpingView.getThread().getState() == Thread.State.RUNNABLE){
    		jumpingView.getThread().resumeGame();
    	}
    	
    }
    
    @Override
    public void onPause(){
    	super.onPause();
    	// @aiyue 0928
    	// mSensorManager.unregisterListener(this);
  	  	jumpingView.getThread().pause(); // pause game when Activity pauses
    }
    
    @Override
    public void onDestroy(){
    	super.onDestroy();
    	// @aiyue 0928
    	sm.unregisterListener(this);
    }
    
    @Override
    public void onStop(){
    	super.onStop();
    	sm.unregisterListener(this);
    }
    
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // just have the View's thread save its state into our Bundle
        super.onSaveInstanceState(outState);
        jumpingView.getThread().saveState(outState);
        Log.w(this.getClass().getName(), "SIS called");
    }
    
    @Override
    public void onRestoreInstanceState(Bundle state){
    	super.onRestoreInstanceState(state);
    	jumpingView.getThread().restoreState(state);
    }

	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// Do nothing.
	}

	public void onSensorChanged(SensorEvent event) {
			
		// @aiyue 0928
		if (event.sensor.getType() == SensorManager.SENSOR_ACCELEROMETER){
//				jumpingView.getThread().doSensorChanged(sensor, event.values);
		}else if(event.sensor.getType() == SensorManager.SENSOR_ORIENTATION){
			jumpingView.getThread().doSensor(event.values[0]);
		}else{
			// Log.d(tag,"----not accelerometer sensor");
		}
			
	}
}